package cn.iocoder.yudao.module.vlog.report.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.vlog.report.dal.dataobject.VLogReportDO;
import cn.iocoder.yudao.module.vlog.report.dal.mysql.VLogReportMapper;
import cn.iocoder.yudao.module.vlog.report.vo.AppVLogReportCreateReqVO;
import cn.iocoder.yudao.module.vlog.report.vo.VLogReportPageReqVO;
import cn.iocoder.yudao.module.vlog.report.vo.VLogReportRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.vlog.ErrorCodeConstants.REPORT_DUPLICATE;

/**
 * 视频举报 Service 实现类
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Service
@Validated
public class VLogReportServiceImpl implements VLogReportService {

    @Resource
    private VLogReportMapper reportMapper;
    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public Long createReport(Long userId, AppVLogReportCreateReqVO reqVO) {
        VLogReportDO reportDO = reportMapper.selectOne(VLogReportDO::getUserId, userId,
                VLogReportDO::getBizType, reqVO.getBizType(), VLogReportDO::getBizId, reqVO.getBizId());
        if (reportDO != null) {
            throw exception(REPORT_DUPLICATE);
        }
        reportDO = BeanUtils.toBean(reqVO, VLogReportDO.class);
        reportDO.setUserId(userId);
        reportMapper.insert(reportDO);
        return reportDO.getId();
    }

    @Override
    public PageResult<VLogReportRespVO> getReportPage(Long userId, VLogReportPageReqVO pageReqVO) {
        PageResult<VLogReportDO> result = reportMapper.selectPage(userId, pageReqVO);
        Set<Long> userIds = result.getList().stream().map(VLogReportDO::getUserId).collect(Collectors.toSet());
        Set<Long> toUserIds = result.getList().stream().map(VLogReportDO::getToUserId).collect(Collectors.toSet());
        List<UserSimpleVO> users = memberUserApi.getUserSimpleList(CollUtil.addAll(userIds, toUserIds));
        PageResult<VLogReportRespVO> data = BeanUtils.toBean(result, VLogReportRespVO.class);
        data.getList().forEach(item -> {
            UserSimpleVO user = users.stream().filter(u -> Objects.equals(u.getId(), item.getUserId())).findFirst().orElse(null);
            item.setUser(user);
            UserSimpleVO toUser = users.stream().filter(u -> Objects.equals(u.getId(), item.getToUserId())).findFirst().orElse(null);
            item.setToUser(toUser);
        });
        return new PageResult<>(data.getList(), data.getTotal());
    }

}
